package com.wuhongyu.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wuhongyu.entity.Message;

public class MyUtil {


	/***
	 * 将每个数据节点的数据取出来，封装到实体类
	 * @param http
	 * @return
	 */
	public static List<Message> getMessage(String http) {
		//取出该网页中所有职位的行
		List<String> list = getTable(http);
		List<Message> messageList = new ArrayList<>();
		String a = "<(\\w+)([^<>]*)>([^<>]*)</\\1>";
		String b = "<a style=\"font-weight: bold\".*target=\"_blank\">(.+?)</a>";
		//对每一行进行正则表达式验证，取出符合要求的数据
		for (String htmlStr : list) {
			// System.out.println(htmlStr);
			Pattern p1 = Pattern.compile(a, Pattern.CASE_INSENSITIVE);
			Matcher m1 = p1.matcher(htmlStr);
			//由于职位名称是xxxx<b>xx</b>xx的形式，所以需要单独进行一次正则
			Pattern p2 = Pattern.compile(b, Pattern.CASE_INSENSITIVE);
			Matcher m2 = p2.matcher(htmlStr);
			String post;
			Message message = new Message();
			try {
				m2.find();
				post = m2.group(1);
				post = post.replaceAll("<[^>]+>", "");
				// System.out.println(post);
				message.setPost(post);
				m1.find();
				//这里post已经放进去了
				m1.find();
				message.setRate(m1.group(3));
				m1.find();
				message.setCompany(m1.group(3));
				m1.find();
				message.setSalary(m1.group(3));
				m1.find();
				message.setAddress(m1.group(3));
				m1.find();
				message.setDate(m1.group(3));

			} catch (Exception e) {
				e.printStackTrace();
				throw new IllegalArgumentException("正则表达式取值失败");
			}
			messageList.add(message);
		}
		return messageList;
	}

	/***
	 * 得到当前页面所有的数据节点
	 * @param http
	 * @return
	 */
	private static List<String> getTable(String http) {
		//每一个节点的开始标记
		String begin = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"853\" class=\"newlist\">";
		//结束标记
		String end = "</tr>";
		List<String> list = new ArrayList<>();
		String rLine = null;
		boolean isNode = false;
		BufferedReader br = null;
		InputStreamReader read = null;
		StringBuilder sb = new StringBuilder();
		HttpURLConnection httpUrlConn = null;
		try {
			URL url = new URL(http);
			// 开始网络请求
			httpUrlConn = (HttpURLConnection) url.openConnection();
			read = new InputStreamReader(httpUrlConn.getInputStream(), "utf-8");
			br = new BufferedReader(read);
			// 读取返回结果
			while ((rLine = br.readLine()) != null) {
				// 判断目标节点是否出现
				if (rLine.contains(begin)) {
					isNode = true;
				}
				// 开始读取
				if (isNode) {
					sb.append(rLine + "\r\n");
					if (rLine.contains(end)) {
						isNode = false;
						list.add(sb.toString());
						sb.delete(0, sb.length());
						continue;
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (read != null) {
						try {
							read.close();
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							if (httpUrlConn != null) {
								httpUrlConn.disconnect();
							}
						}
					}
				}
			}
		}
		return list;
	}

	/**
	 * 得到当前一共检索出的数据总数
	 * @param http
	 * @return
	 */
	public static Integer getPage(String http){
		String begin = "<em>";
		String end = "</em>";
		List<String> list = new ArrayList<>();
		String rLine = null;
		boolean isNode = false;
		BufferedReader br = null;
		InputStreamReader read = null;
		StringBuilder sb = new StringBuilder();
		HttpURLConnection httpUrlConn = null;
		try {
			URL url = new URL(http);
			// 开始网络请求
			httpUrlConn = (HttpURLConnection) url.openConnection();
			read = new InputStreamReader(httpUrlConn.getInputStream(), "utf-8");
			br = new BufferedReader(read);
			// 读取返回结果
			while ((rLine = br.readLine()) != null) {
				// 判断目标节点是否出现
				if (rLine.contains(begin)) {
					isNode = true;
				}
				// 开始读取
				if (isNode) {
					sb.append(rLine + "\r\n");
					if (rLine.contains(end)) {
						String a = "<(\\w+)([^<>]*)>([^<>]+)</\\1>";
						Pattern p1 = Pattern.compile(a, Pattern.CASE_INSENSITIVE);
						Matcher m1 = p1.matcher(rLine);
						m1.find();
						rLine = m1.group(3);
						return Integer.parseInt(rLine);
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (read != null) {
						try {
							read.close();
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							if (httpUrlConn != null) {
								httpUrlConn.disconnect();
							}
						}
					}
				}
			}
		}
		return 0;
	}


}
